package services

import (
	"chat/models"
	"chat/respositories"
	"errors"
	"fmt"
)

func GetUserInfo(user *models.User) error {
	err := respositories.FindUser(user)
	if err != nil {
		fmt.Printf("err: %v\n", err)
	}
	return err
}

func Register(user *models.User) error {
	if user.UserName == "" || user.PassWord == "" {
		return errors.New("username or password is nil")
	}
	err := respositories.FindUserByUserName(user)
	if err == nil {
		return errors.New("username already exists")
	}

	err = respositories.InsertUser(user)
	if err != nil {
		return err
	}
	return nil
}

func Login(user *models.User) error {
	if user.UserName == "" || user.PassWord == "" {
		return errors.New("username or password is nil")
	}
	err := respositories.FindUser(user)
	if err != nil {
		fmt.Printf("err: %v\n", err)
		return err
	}

	//err = util.LoginInRedis(user)
	if err != nil {
		return err
	}
	return nil
}
